﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;

namespace BuildHelper.Work.Tools.Security
{
    public partial class FormItemPick : XtraForm
    {
        private FormItemPick()
        {
            InitializeComponent();
        }
        private bool isFirst = true;
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            if (isFirst)
            {
                isFirst = false;
                layoutProvider1.SaveCurrentLayoutAsDefault();
                layoutProvider1.ApplySavedLayout();
            }
        }
        public void SaveLayout()
        {
            layoutProvider1.SaveLayout();
        }

        private List<SecurityItem> allItems;
        private int max;
        public FormItemPick(List<SecurityItem> items, int max)
        {
            InitializeComponent();
            allItems = items;
            this.max = max;
        }
        private SecurityItem SearchItem
        {
            get;
            set;
        }
        public void LoadItems(List<SecurityItem> items, SecurityItem item, int i)
        {
            this.Text = "Pasująca opcja [ " + i + "/" + max + "]";
            SearchItem = item;
            this.gcMain.DataSource = new BindingList<SecurityItem>(items);
            this.teKod.EditValue = item.GuiCode;
            this.teNazwa.EditValue = item.GuiName;
            this.teNumer.EditValue = item.Number;
            this.teParentName.EditValue = item.ParentGuiName;
            this.teParentCode.EditValue = item.ParentGuiCode;
            this.teParentNumber.EditValue = item.ParentNumber;
            btnPokazWszystkie.Enabled = items.Count < allItems.Count;
            gvMain.ActiveFilter.Clear();

            Item = null;
        }
        public SecurityItem Item
        {
            get;
            private set;
        }
        public bool CancelAll
        {
            get;
            private set;
        }

        private void Close(DialogResult result)
        {
            layoutProvider1.SaveLayout();
            DialogResult = result;
        }
        private void gvMain_DoubleClick(object sender, EventArgs e)
        {
            //btnOk.PerformClick();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            GridView gv = gcMain.FocusedView as GridView;
            if (gv != null)
            {
                SecurityItem i = gv.GetFocusedRow() as SecurityItem;
                if (i != null)
                {
                    Item = i;
                    Close(System.Windows.Forms.DialogResult.OK);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close(System.Windows.Forms.DialogResult.Cancel);
        }

        private void btnCancelAll_Click(object sender, EventArgs e)
        {
            CancelAll = true;
            Close(System.Windows.Forms.DialogResult.Cancel);
        }

        private void gcMain_ViewRegistered(object sender, DevExpress.XtraGrid.ViewOperationEventArgs e)
        {
            if (e.View != gvMain)
            {
                e.View.Assign(gvMain, true);
                GridView gv = e.View as GridView;
                if (gv != null)
                {
                    foreach (GridColumn c in gv.Columns)
                    {
                        if (c.UnboundType == DevExpress.Data.UnboundColumnType.Boolean)
                        {
                            c.UnboundType = DevExpress.Data.UnboundColumnType.Bound;
                            c.UnboundType = DevExpress.Data.UnboundColumnType.Boolean;
                        }
                    }
                }
            }
        }

        private void btnPokazWszystkie_Click(object sender, EventArgs e)
        {
            this.gcMain.DataSource = new BindingList<SecurityItem>(allItems);
        }

        private void gvMain_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
            {
                if (e.Row != null && e.Row is SecurityItem)
                {
                    if (e.Column.FieldName == colMatchCode.FieldName)
                    {
                        e.Value = ((SecurityItem)e.Row).GuiCode == SearchItem.GuiCode;
                    }
                    else if (e.Column.FieldName == colMatchNumber.FieldName)
                    {
                        e.Value = ((SecurityItem)e.Row).Number == SearchItem.Number;
                    }
                    else if (e.Column.FieldName == colMatchParentNumber.FieldName)
                    {
                        e.Value = ((SecurityItem)e.Row).ParentNumber == SearchItem.ParentNumber;
                    }
                    else if (e.Column.FieldName == colMatchParentCode.FieldName)
                    {
                        e.Value = ((SecurityItem)e.Row).ParentGuiCode == SearchItem.ParentGuiCode;
                    }
                    else if (e.Column.FieldName == colMatchParentName.FieldName)
                    {
                        e.Value = ((SecurityItem)e.Row).ParentGuiName == SearchItem.ParentGuiName;
                    }
                }
            }
        }
    }
}
